/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
	"context"
	"fmt"
	"net"
	"net/http"
	"net/url"
	"strings"
	"time"

	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/apimachinery/pkg/util/proxy"
	"k8s.io/apiserver/pkg/endpoints/request"
	"k8s.io/apiserver/pkg/features"
	"k8s.io/apiserver/pkg/registry/rest"
	"k8s.io/apiserver/pkg/util/feature"
	"k8s.io/client-go/kubernetes"
	corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
	"sigs.k8s.io/apiserver-builder-alpha/example/podexec/pkg/kubelet"
	"sigs.k8s.io/apiserver-runtime/pkg/builder/resource"
	"sigs.k8s.io/apiserver-runtime/pkg/util/loopback"
)

var _ resource.ConnectorSubResource = &PodExec{}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type PodExec struct {
	metav1.TypeMeta `json:",inline"`

	// Stdin if true indicates that stdin is to be redirected for the exec call
	Stdin bool

	// Stdout if true indicates that stdout is to be redirected for the exec call
	Stdout bool

	// Stderr if true indicates that stderr is to be redirected for the exec call
	Stderr bool

	// TTY if true indicates that a tty will be allocated for the exec call
	TTY bool

	// Container in which to execute the command.
	Container string

	// Command is the remote command to execute; argv array; not executed within a shell.
	Command []string
}

func (p *PodExec) SubResourceName() string {
	return "exec"
}

func (p PodExec) New() runtime.Object {
	return &PodExec{}
}

func (p *PodExec) Connect(ctx context.Context, id string, options runtime.Object, responder rest.Responder) (http.Handler, error) {
	execOpts, ok := options.(*PodExec)
	if !ok {
		return nil, fmt.Errorf("invalid options object: %#v", options)
	}
	clientConfig := loopback.GetLoopbackClientConfig()
	client, err := kubernetes.NewForConfig(clientConfig)
	nodeConnGetter, err := kubelet.NewNodeConnectionInfoGetter(
		func(name string) (*corev1.Node, error) {
			return client.CoreV1().Nodes().Get(ctx, name, metav1.GetOptions{})
		},
		kubelet.KubeletClientConfig{
			Port:         10250,
			ReadOnlyPort: 10255,
			PreferredAddressTypes: []string{
				// --override-hostname
				string(corev1.NodeHostName),

				// internal, preferring DNS if reported
				string(corev1.NodeInternalDNS),
				string(corev1.NodeInternalIP),

				// external, preferring DNS if reported
				string(corev1.NodeExternalDNS),
				string(corev1.NodeExternalIP),
			},
			EnableHTTPS: true,
			HTTPTimeout: time.Duration(5) * time.Second,
		})
	location, transport, err := ExecLocation(client.CoreV1(), nodeConnGetter, ctx, id, execOpts)
	if err != nil {
		return nil, err
	}
	return newThrottledUpgradeAwareProxyHandler(location, transport, false, true, true, responder), nil
}

func (p *PodExec) NewConnectOptions() (runtime.Object, bool, string) {
	return &PodExec{}, false, ""
}

func (p *PodExec) ConnectMethods() []string {
	return []string{"GET", "POST"}
}

func newThrottledUpgradeAwareProxyHandler(location *url.URL, transport http.RoundTripper, wrapTransport, upgradeRequired, interceptRedirects bool, responder rest.Responder) *proxy.UpgradeAwareHandler {
	handler := proxy.NewUpgradeAwareHandler(location, transport, wrapTransport, upgradeRequired, proxy.NewErrorResponder(responder))
	handler.InterceptRedirects = interceptRedirects && feature.DefaultFeatureGate.Enabled(features.StreamingProxyRedirects)
	handler.RequireSameHostRedirects = feature.DefaultFeatureGate.Enabled(features.ValidateProxyRedirects)
	handler.MaxBytesPerSec = 0
	return handler
}

// ExecLocation returns the exec URL for a pod container. If opts.Container is blank
// and only one container is present in the pod, that container is used.
func ExecLocation(
	getter corev1client.CoreV1Interface,
	connInfo kubelet.ConnectionInfoGetter,
	ctx context.Context,
	name string,
	opts *PodExec,
) (*url.URL, http.RoundTripper, error) {
	return streamLocation(getter, connInfo, ctx, name, opts, opts.Container, "exec")
}

func streamLocation(
	getter corev1client.CoreV1Interface,
	connInfo kubelet.ConnectionInfoGetter,
	ctx context.Context,
	name string,
	opts runtime.Object,
	container,
	path string,
) (*url.URL, http.RoundTripper, error) {
	ns, _ := request.NamespaceFrom(ctx)
	pod, err := getter.Pods(ns).Get(ctx, name, metav1.GetOptions{})
	if err != nil {
		return nil, nil, err
	}

	// Try to figure out a container
	// If a container was provided, it must be valid
	if container == "" {
		switch len(pod.Spec.Containers) {
		case 1:
			container = pod.Spec.Containers[0].Name
		case 0:
			return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name))
		default:
			containerNames := getContainerNames(pod.Spec.Containers)
			initContainerNames := getContainerNames(pod.Spec.InitContainers)
			err := fmt.Sprintf("a container name must be specified for pod %s, choose one of: [%s]", name, containerNames)
			if len(initContainerNames) > 0 {
				err += fmt.Sprintf(" or one of the init containers: [%s]", initContainerNames)
			}
			return nil, nil, errors.NewBadRequest(err)
		}
	} else {
		if !podHasContainerWithName(pod, container) {
			return nil, nil, errors.NewBadRequest(fmt.Sprintf("container %s is not valid for pod %s", container, name))
		}
	}
	nodeName := types.NodeName(pod.Spec.NodeName)
	if len(nodeName) == 0 {
		// If pod has not been assigned a host, return an empty location
		return nil, nil, errors.NewBadRequest(fmt.Sprintf("pod %s does not have a host assigned", name))
	}
	nodeInfo, err := connInfo.GetConnectionInfo(ctx, nodeName)
	if err != nil {
		return nil, nil, err
	}
	params := url.Values{}
	if err := streamParams(params, opts); err != nil {
		return nil, nil, err
	}
	loc := &url.URL{
		Scheme:   nodeInfo.Scheme,
		Host:     net.JoinHostPort(nodeInfo.Hostname, nodeInfo.Port),
		Path:     fmt.Sprintf("/%s/%s/%s/%s", path, pod.Namespace, pod.Name, container),
		RawQuery: params.Encode(),
	}
	return loc, nodeInfo.Transport, nil
}

// getContainerNames returns a formatted string containing the container names
func getContainerNames(containers []corev1.Container) string {
	names := []string{}
	for _, c := range containers {
		names = append(names, c.Name)
	}
	return strings.Join(names, " ")
}

func podHasContainerWithName(pod *corev1.Pod, containerName string) bool {
	var hasContainer = false
	for _, c := range pod.Spec.Containers {
		if c.Name == containerName {
			hasContainer = true
		}
	}
	return hasContainer
}

func streamParams(params url.Values, opts runtime.Object) error {
	switch opts := opts.(type) {
	case *PodExec:
		if opts.Stdin {
			params.Add(corev1.ExecStdinParam, "1")
		}
		if opts.Stdout {
			params.Add(corev1.ExecStdoutParam, "1")
		}
		if opts.Stderr {
			params.Add(corev1.ExecStderrParam, "1")
		}
		if opts.TTY {
			params.Add(corev1.ExecTTYParam, "1")
		}
		for _, c := range opts.Command {
			params.Add("command", c)
		}
	default:
		return fmt.Errorf("Unknown object for streaming: %v", opts)
	}
	return nil
}
